<script setup>
    import { ref,onMounted } from 'vue';
    import {quaryPageApi} from '@/api/user'

const searchEmp = ref({
  name: '',
  gender: ''
})

//列表的数据定义
const empList=ref([])


//定义分页数据
const currentPage=ref(1)
const pageSize = ref(3)
//总条数
const total = ref(0)
const size= 'default'
const disabled = false
const background = true 

//分页每页大小发生改变（每页展示val条数据）
const handleSizeChange = (val) => {
  console.log(`${val} items per page`)
  //页码从第一页开始
  currentPage.value=1
  //每页多少条
  pageSize.value=val
  //执行搜索
  searchHandler()
}
//当前页发生改变（当前是第val页）
const handleCurrentChange = (val) => {
  console.log(`current page: ${val}`)
  //当前页发生变化
  currentPage.value=val
  //执行搜索
  searchHandler()
}

//执行分页搜索函数
const searchHandler=async()=>{
  //调用接口
  let result = await quaryPageApi(searchEmp.value.name,searchEmp.value.gender,currentPage.value,pageSize.value)
  //放数据
  empList.value = result.data.userList
  //分页数据完善
  total.value = result.data.total
}

//清空操作
const clearHandler = ()=> {
  searchEmp.value.gender=''
  searchEmp.value.name=''
  searchHandler()
}

onMounted(()=>{
  searchHandler()
})
</script>

<template>
     <el-form :inline="true" :model="searchEmp" label-width="auto" style="max-width: 800px">
    <el-form-item label="姓名" style="width: 200px;">
      <el-input v-model="searchEmp.name" />
    </el-form-item>
    <el-form-item label="性别" style="width: 200px;">
      <el-select v-model="searchEmp.gender" placeholder="性别">
        <el-option label="男" value="1" />
        <el-option label="女" value="2" />
      </el-select>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="searchHandler">查询</el-button>
    </el-form-item>
    <el-form-item>
      <el-button type="danger" @click="clearHandler">清空</el-button>
    </el-form-item>
  </el-form>
  
   <el-table :data="empList"  style="width: 100%">
    <el-table-column prop="name" label="姓名" width="180" />
    <el-table-column prop="gender" label="性别" width="180" >
      <template #default="scope">
        <el-tag v-if="scope.row.gender==1">男</el-tag>
        <el-tag v-else>女</el-tag>
      </template>
    </el-table-column>
    <el-table-column prop="createTime" label="创建日期" />
  </el-table>


   <!-- 分页实现 -->
  <div class="page">
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[3, 6, 9, 12]"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<style scoped>

</style>